package com.fzq.exception;

import com.fzq.util.ResponseResult;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpServerErrorException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * springmvc全局异常处理类
 *  该类中需要程序员将不同的异常提示定义好
 */
@ControllerAdvice  //表示该类是对controller类做增强的   ()
public class GlobalException{

    /**
     * 400 - Bad Request
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public ResponseResult<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return new ResponseResult<Void>(400,"参数解析失败");
    }

    /**
     * 405 - Method Not Allowed
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseResult<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return new ResponseResult<Void>(405,"不支持当前请求的方法");
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public ResponseResult<Void> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return new ResponseResult<Void>(415,"不支持当前媒体类型");
    }

    /**
     * 500 - Internal Server Error
     */
    @ExceptionHandler(HttpServerErrorException.class)
    @ResponseBody
    public ResponseResult<Void> handleServerErrorException(HttpServerErrorException e) {
        return new ResponseResult<Void>(500,"服务器异常");
    }


    /**
     * 表单验证异常
     */
    @ExceptionHandler(ValidatorException.class)
    @ResponseBody
    public ResponseResult<Object> handleValidatorException(ValidatorException e) {
        //取出BindingResult中的异常信息并保存到错误对象中返回给页面
        Map<String,String> beanError=new HashMap<String,String>();
        List<FieldError> list = e.getResult().getFieldErrors();
        for (FieldError error : list) {
            beanError.put(error.getField(),error.getDefaultMessage());
        }
        //将错误对象发送到页面
        return new ResponseResult<>(416,"表单不符合验证规则",beanError);
    }
}
